package com.jinkaimen.goodcash.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.activity.SelectListActivity;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.goodcash.R;
import com.jinkaimen.goodcash.bean.GetShopList;
import com.jinkaimen.goodcash.bean.ShopBean;
import com.jinkaimen.goodcash.dao.AccountDao;
import com.jinkaimen.goodcash.manager.ApiManager;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class SaleDetailActivity extends BaseActivity {

    private static final int REQUEST_CODE_SELECT_SHOP = 0;

    private boolean isSaleLeader;
    private TextView mSaleLeaderView;
    private TextView mSaleView;
    private EditText mNameView;
    private EditText mTelView;
    private List<ShopBean> mDataList = new ArrayList<>();
    private ArrayList<String> mShopList = new ArrayList<>();
    private int mPosition;
    private TextView mShopNameView;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("新增店员");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_sale_detail;
    }

    @Override
    protected void initChildView() {
        mSaleLeaderView = (TextView) findViewById(R.id.tv_activity_sale_detail_sale_leader);
        mSaleView = (TextView) findViewById(R.id.tv_activity_sale_detail_sale);


        mNameView = (EditText) findViewById(R.id.et_activity_sale_detail_name);
        mTelView = (EditText) findViewById(R.id.et_activity_sale_detail_tel);
        mShopNameView = (TextView) findViewById(R.id.rv_activity_sale_detail_shop_name);
    }

    @Override
    protected void initChildData() {
        ApiManager.getApi().shopList(
                "HSY001010",
                AccountDao.getInstance().getAccount().id
        ).enqueue(new NetCallBack<GetShopList>() {
            @Override
            protected void onSuccess(Call<GetShopList> call, Response<GetShopList> response) {
                GetShopList getShopList = response.body();
                mDataList.clear();
                mDataList.addAll(getShopList.shopList);

                mShopList.clear();
                for (int i = 0; i < mDataList.size(); i++) {
                    ShopBean shopBean = mDataList.get(i);
                    mShopList.add(shopBean.name);
                }
            }

            @Override
            protected void hiddenProgress() {

            }
        });
    }

    @Override
    protected void setChildViewListener() {
        findViewById(R.id.rl_activity_identification_shop_step1_select_location).setOnClickListener(this);
        findViewById(R.id.btn_activity_identification_shop).setOnClickListener(this);


        mSaleLeaderView.setOnClickListener(this);
        mSaleView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.rl_activity_identification_shop_step1_select_location:
                Bundle bundle = new Bundle();
                bundle.putString(SelectListActivity.CENTER_TITLE, "选择店铺");
                bundle.putStringArrayList(SelectListActivity.DATA_LIST, mShopList);
                ActivityUtils.startActivityForResult(this, SelectListActivity.class, bundle, REQUEST_CODE_SELECT_SHOP);
                break;
            case R.id.btn_activity_identification_shop:
                showLoadingDialogCanotCancle();
                ApiManager.getApi().saveSale(
                        "HSY001019",
                        isSaleLeader?2:3,
                        mTelView.getText().toString(),
                        mNameView.getText().toString(),
                        AccountDao.getInstance().getAccount().id,
                        mDataList.get(mPosition).id
                ).enqueue(new NetCallBack<String>() {
                    @Override
                    protected void onSuccess(Call<String> call, Response<String> response) {
                        UIUtils.showToast("保存成功");
                        ActivityUtils.finishActivity(SaleDetailActivity.this);
                    }

                    @Override
                    protected void hiddenProgress() {
                        hideLodingDialog();
                    }
                });
                break;
            case R.id.tv_activity_sale_detail_sale_leader:
                isSaleLeader = true;
                mSaleLeaderView.setBackgroundResource(R.drawable.shape_label_blue);
                mSaleView.setBackgroundResource(R.drawable.shape_label_gray);

                mSaleLeaderView.setTextColor(Color.parseColor("#6284dd"));
                mSaleView.setTextColor(Color.parseColor("#bebebe"));
                break;
            case R.id.tv_activity_sale_detail_sale:
                isSaleLeader = false;
                mSaleView.setBackgroundResource(R.drawable.shape_label_blue);
                mSaleLeaderView.setBackgroundResource(R.drawable.shape_label_gray);

                mSaleView.setTextColor(Color.parseColor("#6284dd"));
                mSaleLeaderView.setTextColor(Color.parseColor("#bebebe"));
                break;
        }
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            switch (requestCode){
                case REQUEST_CODE_SELECT_SHOP:
                    mPosition = data.getIntExtra("data", 0);
                    mShopNameView.setText(mDataList.get(mPosition).name);
                    break;
            }
        }
    }
}
